<template>
  <div class="comment-container">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <!-- 面包屑,路径导航 -->
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
          <el-breadcrumb-item>{{"评论管理"}}</el-breadcrumb-item>
        </el-breadcrumb>
      </div>
      <!-- 表格data  -->
      <el-table :data="articles" style="width: 100%" stripe class="tableList">
        <el-table-column prop="title" label="标题"></el-table-column>
        <el-table-column prop="total_comment_count" label="总评论数"></el-table-column>
        <el-table-column prop="fans_comment_count" label="粉丝评论数"></el-table-column>
        <el-table-column prop="address" label="评论状态">
          <template slot-scope="scope">{{scope.row.comment_status? '正常':'关闭'}}</template>
        </el-table-column>
        <el-table-column prop="address" label="操作">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.comment_status"
              active-color="#99999"
              inactive-color="#99999"
              @change="onStatusChange(scope.row)"
              :disabled="scope.row.statusDisabled"
            ></el-switch>
          </template>
        </el-table-column>
      </el-table>
      <!-- 绑定页码
        current-page ：激活的页码

      :page-size ： 每页大小-->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page.sync="page"
        :page-sizes="[5, 10, 20]"
        :page-size.sync="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="totalCount"
        background
      ></el-pagination>
    </el-card>
  </div>
</template>

<script>
import { getArticles, updateCommentStatus } from "@/api/article";
export default {
  name: "",
  components: {},
  props: {},
  data() {
    return {
      tableData: [
        {
          date: "2016-05-02",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1518 弄"
        },
        {
          date: "2016-05-04",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1517 弄"
        },
        {
          date: "2016-05-01",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1519 弄"
        },
        {
          date: "2016-05-03",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1516 弄"
        }
      ],
      currentPage1: 5,
      currentPage2: 5,
      currentPage3: 5,
      currentPage4: 4,
      totalCount: 0, //总数据条数
      pageSize: 10, // 每页多少条
      page: 1, // 当前激活页码
      articles: [] // 文章数据列表
    };
  },
  computed: {},
  watch: {},
  created() {
    this.loadArticles();
  },
  mounted() {},
  methods: {
    handleSizeChange(size) {
      this.loadArticles(1);
    },
    // 页码改变函数
    handleCurrentChange(page) {
      this.loadArticles(page);
    },
    // 状态开关
    onStatusChange(article) {
      //  请求提交数据
      article.statusDisabled = true; // 禁用开关
      updateCommentStatus(article.id, article.comment_status).then(res => {
        // console.log(res);
        article.statusDisabled = false;
        this.$message({
          type: "success",
          message: article.comment_status ? "开启成功" : "关闭成功"
        });
      });
    },
    loadArticles(page = 1) {
      // 让激活页码和数据页码保持一致
      this.page = page;
      getArticles({
        response_type: "comment",
        page,
        per_page: this.pageSize
      }).then(res => {
        // console.log(res)
        const { results } = res.data.data;
        results.forEach(article => {
          article.statusDisabled = false;
        });
        this.articles = res.data.data.results;
        this.totalCount = res.data.data.total_count;
      });
    }
  }
};
</script>

<style scoped lang="less">
.tableList {
  margin-bottom: 30px;
}
</style>